﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar.SuperGrid;
using ESRI.ArcGIS.Geodatabase;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 扩展控件的方法
    /// </summary>
    public static class ControlExtension
    {
        /// <summary>
        /// 根据要素类对象在数据列表控件中添加列
        /// </summary>
        /// <param name="grid">数据列表对象</param>
        /// <param name="featcls">要素类</param>
        /// <param name="hidename">是否添加一个指定名称的隐藏文本列</param>
        [Obsolete("已废弃")]
        public static void AddColumnsByFeatureClass(this SuperGridControl grid, IFeatureClass featcls, string hidename = "")
        {
            IFields fields = featcls.Fields;
            IField fieldLength = null;
            IField fieldArea = null;
            try
            {
                fieldLength = featcls.LengthField;
                fieldArea = featcls.AreaField;
            }
            catch
            {
            }
            for (int i = 0; i < fields.FieldCount; i++)
            {
                IField field = fields.get_Field(i);
                GridColumn col = new GridColumn();
                col.HeaderText = field.AliasName;
                col.Name = field.Name;
                col.DataPropertyName = field.Name;
                col.ReadOnly = true;
                col.AllowEdit = false;
                col.EditorType = typeof(GridTextBoxXEditControl);
                col.FilterEditType = FilterEditType.Auto;
                //三个固有字段
                if (fieldLength != null && field.Equals(fieldLength))
                {
                    col.HeaderText = featcls.AreaField == null ? "长度" : "周长度";
                    col.EditorType = typeof(GridDoubleInputEditControl);
                }
                else if (fieldArea != null && field.Equals(fieldArea))
                {
                    col.HeaderText = "面积";
                    col.EditorType = typeof(GridDoubleInputEditControl);
                }
                else if (field.Name.Equals(featcls.ShapeFieldName))
                {
                    col.HeaderText = "图形";
                    col.Visible = false;
                }
                else
                {
                    BindColumnAttribute(col, field.Type);
                }
                grid.PrimaryGrid.Columns.Add(col);
            }
            if (!string.IsNullOrWhiteSpace(hidename))
            {
                GridColumn col = new GridColumn();
                col.HeaderText = hidename;
                col.Name = hidename;
                col.DataPropertyName = hidename;
                col.ReadOnly = true;
                col.AllowEdit = false;
                col.EditorType = typeof(GridTextBoxXEditControl);
                col.FilterEditType = FilterEditType.Auto;
                col.Visible = false;
                grid.PrimaryGrid.Columns.Add(col);
            }
        }

        /// <summary>
        /// 根据要素类对象在数据列表控件中添加列
        /// </summary>
        /// <param name="grid">数据列表对象</param>
        /// <param name="fields"></param>
        /// <param name="lenfield"></param>
        /// <param name="areafield"></param>
        /// <param name="hidename">是否添加一个指定名称的隐藏文本列</param>
        public static void AddColumns(this SuperGridControl grid, IFields fields, IField lenfield, IField areafield, string hidename = "")
        {
            if (fields == null) return;
            int cnt = fields.FieldCount;
            for (int i = 0; i < cnt; i++)
            {
                IField field = fields.Field[i];
                GridColumn col = new GridColumn();
                col.HeaderText = field.AliasName?? field.Name;
                col.Name = field.Name;
                col.DataPropertyName = field.Name;
                col.ReadOnly = true;
                col.AllowEdit = false;
                col.EditorType = typeof(GridTextBoxXEditControl);
                col.FilterEditType = FilterEditType.Auto;
                //三个固有字段
                if (lenfield != null && field.Equals(lenfield))
                {
                    col.HeaderText = "长度";
                    //col.EditorType = typeof(GridDoubleInputEditControl);
                    col.EditorType = typeof(GridTextBoxXEditControl);
                    
                }
                else if (areafield != null && field.Equals(areafield))
                {
                    col.HeaderText = "面积";
                    col.EditorType = typeof(GridTextBoxXEditControl);
                }
                else
                {
                    BindColumnAttribute(col, field.Type);
                }
                grid.PrimaryGrid.Columns.Add(col);
            }
            if (!string.IsNullOrWhiteSpace(hidename))
            {
                GridColumn col = new GridColumn();
                col.HeaderText = hidename;
                col.Name = hidename;
                col.DataPropertyName = hidename;
                col.ReadOnly = true;
                col.AllowEdit = false;
                col.EditorType = typeof(GridTextBoxXEditControl);
                col.FilterEditType = FilterEditType.Auto;
                col.Visible = false;
                grid.PrimaryGrid.Columns.Add(col);
            }
        }
        
        /// <summary>
        /// 添加字段到属性列表中
        /// </summary>
        /// <param name="grid">属性列表</param>
        /// <param name="field">要添加的字段</param>
        public static void AddColumn(this SuperGridControl grid, IField field)
        {
            GridColumn col = new GridColumn();
            col.HeaderText = field.AliasName;
            col.Name = field.Name;
            col.DataPropertyName = field.Name;
            col.ReadOnly = true;
            col.AllowEdit = false;
            col.EditorType = typeof(GridTextBoxXEditControl);
            col.FilterEditType = FilterEditType.Auto;
            BindColumnAttribute(col, field.Type);
            grid.PrimaryGrid.Columns.Add(col);
        }
        
        /// <summary>
        /// 根据字段类型绑定列的属性
        /// </summary>
        /// <param name="column">要绑定的列</param>
        /// <param name="type">字段类型</param>
        private static void BindColumnAttribute(GridColumn column, esriFieldType type)
        {
            if (type == esriFieldType.esriFieldTypeBlob)
            {
                column.EditorType = typeof(GridTextBoxXEditControl);
            }
            else if (type == esriFieldType.esriFieldTypeDate)
            {
                column.EditorType = typeof(GridDateTimePickerEditControl);
                column.AllowEdit = true;
            }
            else if (type == esriFieldType.esriFieldTypeDouble)
            {
                column.EditorType = typeof(GridDoubleInputEditControl);
                column.RenderType = typeof(GridDoubleInputEditControl);
                column.AllowEdit = true;
            }
            else if (type == esriFieldType.esriFieldTypeGeometry)
            {
                column.HeaderText = "图形";
                column.Visible = false;
                column.EditorType = typeof(GridTextBoxXEditControl);
            }
            else if (type == esriFieldType.esriFieldTypeInteger)
            {
                column.EditorType = typeof(GridIntegerInputEditControl);
                column.AllowEdit = true;
            }
            else if (type == esriFieldType.esriFieldTypeOID)
            {
                column.HeaderText = "序号";
            }
            else if (type == esriFieldType.esriFieldTypeSingle)
            {
                column.EditorType = typeof(GridIntegerInputEditControl);
                column.AllowEdit = true;
            }
            else if (type == esriFieldType.esriFieldTypeSmallInteger)
            {
                column.EditorType = typeof(GridIntegerInputEditControl);
                column.AllowEdit = true;
            }
            else if (type == esriFieldType.esriFieldTypeString)
            {
                column.AllowEdit = true;
            }
        }

    }
}